#include <onix/types.h>
#include <string.h>

#define SEPARATOR1 '/'
#define SEPARATOR2 '\\'
#define IS_SEPARATOR(c) (c == SEPARATOR1 || c == SEPARATOR2)

char* strcpy(char* dest, const char* src)
{
    char* ptr = dest;
    while (true)
    {
        *(ptr++) = *src;
        if (*(src++) == EOS)
            return dest;
    }
}

char* strncpy(char* dest, const char* src, size_t count)
{
    char* ptr = dest;
    for (size_t nr = 0; nr < count; nr++)
    {
        *(ptr++) = *src;
        if (*(src++) == EOS)
            return dest;
    }
    dest[count - 1] = EOS;
    return dest;
}

char* strcat(char* dest, const char* src)
{
    char* ptr = dest;
    while (*ptr != EOS)
    {
        ptr++;
    }
    while (true)
    {
        *(ptr++) = *src;
        if (*(src++) == EOS)
            return dest;
    }
}

size_t strlen(const char* str)
{
    char* ptr = (char*)str;
    while (*ptr != EOS)
    {
        ptr++;
    }
    return ptr - str;
}

int strcmp(const char* lhs, const char* rhs)
{
    while (*lhs == *rhs && *lhs != EOS && *rhs != EOS)
    {
        lhs++;
        rhs++;
    }
    return (*lhs < *rhs) ? (-1) : (*lhs > *rhs);
}

char* strchr(const char* str, int ch)
{
    char* ptr = (char*)str;
    while (true)    
    {
        if (*ptr == ch)
            return ptr;
        if (*(ptr++) == EOS)
            return NULL;
    }
}

char* strrchr(const char* str, int ch)
{
    char* last = NULL;
    char* ptr = (char*)str;
    while (true)
    {
        if (*ptr == ch)
            last = ptr;
        if (*(ptr++) == EOS)
            return last;
    }
}

int memcmp(const void* lhs, const void* rhs, size_t count)
{
    char* lptr = (char*)lhs;
    char* rptr = (char*)rhs;
    while ((count > 0) && *lptr == *rptr)
    {
        lptr++;
        rptr++;
        count--;
    }
    if (count == 0)
        return 0;
    return (*lptr < *rptr) ? (-1) : (*lptr < *rptr);
}

void* memset(void* dest, int ch, size_t count)
{
    char* ptr = dest;
    while (count--)
    {
        *(ptr++) = ch;
    }
    return dest;
}

void *memcpy(void *dest, const void *src, size_t count)
{
    char *ptr = dest;
    while (count--)
    {
        *ptr++ = *((char *)(src++));
    }
    return dest;
}

void* memchr(const void* str, int ch, size_t count)
{
    char* ptr = (char*)str;
    while (count--)
    {
        if (*ptr == ch)
            return (void*)ptr;
        ptr++;
    }
}

// 获得第一个文件分隔符
char* strsep(const char* str)
{
    char* ptr =  (char*)str;
    while (true)
    {
        if (IS_SEPARATOR(*ptr))
            return ptr;
        if (*(ptr++) == EOS)
            return NULL;
    }
}

// 获取最后一个分隔符
char* strrsep(const char* str)
{
    char* last = NULL;
    char* ptr = (char*)str;
    while (true)
    {
        if (IS_SEPARATOR(*ptr))
            last = ptr;
        if (*(ptr++) == EOS)
            return last;
    }
}